package com.iyundao.io.mapper;

import com.iyundao.io.model.SysUser;
import com.iyundao.io.model.SysUserProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by black on 2019/4/22.
 */
@Repository
public interface SysUserSqlMapper {
    @Select({
            "select",
            "sid, login_name, show_name, real_name, login_pwd, phone, card_no, email, birthday, ",
            "sex, address, flag, create_date, create_id, orde_by",
            "from sys_user",
            "where login_name = #{loginName,jdbcType=VARCHAR} "
    })
    @Results({
            @Result(column="sid", property="sid", jdbcType=JdbcType.CHAR, id=true),
            @Result(column="login_name", property="loginName", jdbcType=JdbcType.VARCHAR),
            @Result(column="show_name", property="showName", jdbcType=JdbcType.VARCHAR),
            @Result(column="real_name", property="realName", jdbcType=JdbcType.VARCHAR),
            @Result(column="login_pwd", property="loginPwd", jdbcType=JdbcType.VARCHAR),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="card_no", property="cardNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
            @Result(column="birthday", property="birthday", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="sex", property="sex", jdbcType=JdbcType.VARCHAR),
            @Result(column="address", property="address", jdbcType=JdbcType.VARCHAR),
            @Result(column="detailed", property="detailed", jdbcType=JdbcType.VARCHAR),
            @Result(column="flag", property="flag", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="create_id", property="createId", jdbcType=JdbcType.CHAR),
            @Result(column="orde_by", property="ordeBy", jdbcType=JdbcType.TINYINT)
    })
    SysUserProvider selectUserByLoginName(String login_name);

    @Select(" select * from sys_user su " +
            "LEFT JOIN user_dept ud " +
            "on ud.uid = su.sid " +
            "LEFT JOIN user_role ur " +
            "on ur.uid = su.sid " +
            "where su.flag='1' and ur.rid='3' and ud.did=#{deptId,jdbcType=VARCHAR}"
    )
    @Results({
            @Result(column="sid", property="sid", jdbcType=JdbcType.CHAR, id=true),
            @Result(column="login_name", property="loginName", jdbcType=JdbcType.VARCHAR),
            @Result(column="show_name", property="showName", jdbcType=JdbcType.VARCHAR),
            @Result(column="real_name", property="realName", jdbcType=JdbcType.VARCHAR),
            @Result(column="login_pwd", property="loginPwd", jdbcType=JdbcType.VARCHAR),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="card_no", property="cardNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
            @Result(column="birthday", property="birthday", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="sex", property="sex", jdbcType=JdbcType.VARCHAR),
            @Result(column="address", property="address", jdbcType=JdbcType.VARCHAR),
            @Result(column="detailed", property="detailed", jdbcType=JdbcType.VARCHAR),
            @Result(column="flag", property="flag", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="create_id", property="createId", jdbcType=JdbcType.CHAR),
            @Result(column="orde_by", property="ordeBy", jdbcType=JdbcType.TINYINT)
    })
    List<SysUser> selectUserByDeptId(String deptId);


    @Select(" select * from sys_user su " +
            "LEFT JOIN user_dept ud " +
            "on ud.uid = su.sid " +
            "LEFT JOIN user_role ur " +
            "on ur.uid = su.sid " +
            "where su.flag='1' and ur.rid='2' and ud.did=#{deptId,jdbcType=VARCHAR}"
    )
    @Results({
            @Result(column="sid", property="sid", jdbcType=JdbcType.CHAR, id=true),
            @Result(column="login_name", property="loginName", jdbcType=JdbcType.VARCHAR),
            @Result(column="show_name", property="showName", jdbcType=JdbcType.VARCHAR),
            @Result(column="real_name", property="realName", jdbcType=JdbcType.VARCHAR),
            @Result(column="login_pwd", property="loginPwd", jdbcType=JdbcType.VARCHAR),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="card_no", property="cardNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
            @Result(column="birthday", property="birthday", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="sex", property="sex", jdbcType=JdbcType.VARCHAR),
            @Result(column="address", property="address", jdbcType=JdbcType.VARCHAR),
            @Result(column="detailed", property="detailed", jdbcType=JdbcType.VARCHAR),
            @Result(column="flag", property="flag", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="create_id", property="createId", jdbcType=JdbcType.CHAR),
            @Result(column="orde_by", property="ordeBy", jdbcType=JdbcType.TINYINT)
    })
    List<SysUser> selectAdministratorByDeptId(String deptId);

    @Select({
            "select sys_role.sid as rid ,sys_role.show_name as rname, sys_user.* from sys_user " +
                    "LEFT JOIN user_role on user_role.uid = sys_user.sid " +
                    "LEFT JOIN sys_role on sys_role.sid = user_role.rid and sys_role.flag = '1' " +
                    "where sys_user.sid not in (select uid from user_dept) and sys_user.flag = '1' " +
                    "ORDER BY sys_user.orde_by ASC"

    })
    @Results({
            @Result(column="sid", property="sid", jdbcType=JdbcType.CHAR, id=true),
            @Result(column="rid", property="rid", jdbcType=JdbcType.CHAR),
            @Result(column="rname", property="rname", jdbcType=JdbcType.VARCHAR),
            @Result(column="login_name", property="loginName", jdbcType=JdbcType.VARCHAR),
            @Result(column="show_name", property="showName", jdbcType=JdbcType.VARCHAR),
            @Result(column="real_name", property="realName", jdbcType=JdbcType.VARCHAR),
            @Result(column="login_pwd", property="loginPwd", jdbcType=JdbcType.VARCHAR),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="card_no", property="cardNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
            @Result(column="birthday", property="birthday", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="sex", property="sex", jdbcType=JdbcType.VARCHAR),
            @Result(column="address", property="address", jdbcType=JdbcType.VARCHAR),
            @Result(column="detailed", property="detailed", jdbcType=JdbcType.VARCHAR),
            @Result(column="flag", property="flag", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="create_id", property="createId", jdbcType=JdbcType.CHAR),
            @Result(column="orde_by", property="ordeBy", jdbcType=JdbcType.TINYINT)
    })
    List<Map> selectUserByNotInDept();
}
